package com.tgy.algorithm.base;


import java.util.EmptyStackException;
import java.util.Stack;

class MyStackO1 {

    private Stack<Integer> stack = new Stack<>();
    private Stack<Integer> minstack = new Stack<>();

    MyStackO1() {

    }

    public void push(int x) {
//        stack.push(x);
        stack.push(x);
        if (minstack.isEmpty()) {
            minstack.push(x);
        }else {
          int val = minstack.peek();
          if(val > x) {
              minstack.push(x);
          }else {
              minstack.push(val);
          }
        }
    }

    public int pop() {
        if (minstack.isEmpty()) {
            throw new EmptyStackException();
        }
        int val = stack.pop();
        minstack.pop();
        return val;
    }

    public int getMin() {
        return minstack.peek();
    }

    public void print() {
        System.out.println(minstack);
    }
}

public class _获取一个栈的最小值时间是O_1 {

    public static void main(String[] args) {
        MyStackO1 myStackO1 = new MyStackO1();
        myStackO1.push(4);
        myStackO1.push(3);
        myStackO1.push(5);
        System.out.println(myStackO1.getMin());
        myStackO1.pop();
        myStackO1.print();
        System.out.println(myStackO1.getMin());
    }
}
